<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GrPPI: Farm pattern</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">0.2</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Farm pattern </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <b>farm</b> pattern (or <em>task-farm</em>) is a streaming pattern that applies an operation to every element in a stream.</p>
<p>The interface to the <b>farm</b> pattern is provided by function <code><a class="el" href="group__farm__pattern.html#ga26bdb7c1b6c3d4bedbcfc1774d00833c" title="Invoke Farm pattern on a data stream that can be composed in other streaming patterns. ">grppi::farm()</a></code>. As all functions in <em>GrPPI</em>, this function takes as its first argument an execution policy.</p>
<div class="fragment"><div class="line"><a class="code" href="group__farm__pattern.html#ga26bdb7c1b6c3d4bedbcfc1774d00833c">grppi::farm</a>(exec, other_arguments...);</div></div><!-- fragment --><h2>Farm variants</h2>
<p>There are several variants:</p>
<ul>
<li>Stand-alone <em>transforming farm</em>: Applies a transformation to a stream of values produced by a generator and invokes a consumer for each generated result.</li>
<li>Stand-alone <em>identity farm</em>: Consumes a stream of values produced by a generator.</li>
<li>Composable <em>farm</em>: Defines a farm that can be used as a building block by another pattern (e.g. a <em>pipeline</em>).</li>
</ul>
<h2>Key elements in a farm</h2>
<p>The key elements of a <b>farm</b> are the <b>Generator</b>, the <b>Transformer</b>, and the <b>Consumer</b>.</p>
<p>The central element in a farm is the <b>Transformer</b>. The operation may be any C++ callable entity. This operation, is a unary operation taking a data item and returning its transformation. Thus, a transformer <code>op</code> is any operation that, given an input value <code>x</code> of type <code>T</code> makes valid the following:</p>
<div class="fragment"><div class="line">U res{transformer(x)};</div></div><!-- fragment --><p>A <em>stand-alone farm</em> also has a <b>Generator</b>. The generator may be any C++ callable entity that produces values in a way that allows to signal an indication of end-of stream. For this purpose, the farm requires that the generator produces values of any type that is convertible to <code>bool</code> and can be dereferenced:</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> r = generator(); <span class="comment">// r is a value generated by a generator.</span></div><div class="line"></div><div class="line"><span class="keywordflow">if</span> (!r) { <span class="comment">/* ... */</span> } <span class="comment">// !r is convertible to bool</span></div><div class="line"></div><div class="line"><span class="keywordflow">if</span> (r) {              <span class="comment">// r is convertible to bool</span></div><div class="line">  <span class="keyword">auto</span> y = *r;        <span class="comment">// r can be dereferenced.</span></div><div class="line">}</div></div><!-- fragment --><p>Additionally a <em>farm</em> may have a <b>Consumer</b>. The consumer may be any C++ callable entity that takes values of the result type of the <em>Transformer</em>.</p>
<div class="fragment"><div class="line"><span class="keyword">auto</span> t = transformer(x);</div><div class="line">consumer(t);</div></div><!-- fragment --><h2>Details on farm variants</h2>
<h3>Stand-alone transforming farm</h3>
<p>A <em>stand-alone transforming farm</em> transforms each generated element in a stream and then consumes it. </p><hr/>
<p> <b>Example</b>: Generate a sequence of integers and print the square of each value. </p><div class="fragment"><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacestd.html">std</a>;</div><div class="line"><span class="keyword">using namespace </span>experimental;</div><div class="line"><span class="comment">//...</span></div><div class="line"><span class="keywordtype">int</span> n = 10;</div><div class="line"><a class="code" href="group__farm__pattern.html#ga26bdb7c1b6c3d4bedbcfc1774d00833c">grppi::farm</a>(exec,</div><div class="line">  []() -&gt; optional&lt;int&gt; {</div><div class="line">    n--;</div><div class="line">    <span class="keywordflow">if</span> (n&gt;0) <span class="keywordflow">return</span> n;</div><div class="line">    <span class="keywordflow">else</span>     {};</div><div class="line">  },</div><div class="line">  [](<span class="keywordtype">int</span> x) { <span class="keywordflow">return</span> x*x; },</div><div class="line">  [](<span class="keywordtype">int</span> x) {</div><div class="line">    cout &lt;&lt; x &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div><div class="line">  }</div><div class="line">);</div></div><!-- fragment --><hr/>
<h3>Stand-alone identity farm</h3>
<p>A <em>stand-alone identity farm</em> generates a stream of data items and then consumes them. </p><hr/>
<p> <b>Example</b>: Read a file and write to the standard output one word per line. </p><div class="fragment"><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacestd.html">std</a>;</div><div class="line"><span class="keyword">using namespace </span>experimental;</div><div class="line"><span class="comment">//...</span></div><div class="line">ifstream file{<span class="stringliteral">&quot;file.txt&quot;</span>};</div><div class="line"><a class="code" href="group__farm__pattern.html#ga26bdb7c1b6c3d4bedbcfc1774d00833c">grppi::farm</a>(exec,</div><div class="line">  [&amp;file]() -&gt; optional&lt;string&gt; {</div><div class="line">    <span class="keywordtype">string</span> word;</div><div class="line">    file &gt;&gt; word;</div><div class="line">    <span class="keywordflow">if</span> (file) <span class="keywordflow">return</span> word;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">return</span> {};</div><div class="line">  },</div><div class="line">  [](<span class="keyword">auto</span> word) {</div><div class="line">    cout &lt;&lt; word &lt;&lt; <span class="stringliteral">&quot; , &quot;</span> &lt;&lt; word.length() &lt;&lt; endl;</div><div class="line">  },</div><div class="line">);</div></div><!-- fragment --><hr/>
<h3>Composable farm</h3>
<p>A <em>composable farm</em> applies a <b>Transformer</b> to each data item in a stream. The <b>farm</b> can be inserted into another upper level pattern (which will be responsible for generation and consumption) </p><hr/>
<p> <b>Example</b>: Use a farm as a stage of a composed pipeline. </p><div class="fragment"><div class="line"><a class="code" href="group__pipeline__pattern__native.html#ga50396cad2879f1facd37757ea3aa564b">grppi::pipeline</a>(exec,</div><div class="line">  stageA,</div><div class="line">  stageB,</div><div class="line">  <a class="code" href="group__farm__pattern.html#ga26bdb7c1b6c3d4bedbcfc1774d00833c">grppi::farm</a>(exec, [](<span class="keyword">auto</span> x) {</div><div class="line">    <span class="keywordflow">return</span> x.length();</div><div class="line">  }),</div><div class="line">  stageC</div><div class="line">);</div></div><!-- fragment --><hr/>
<p> <b>Note</b>: For brevity we do not show here the details of other stages.</p>
<p>For composing complex patterns, the <code><a class="el" href="group__farm__pattern.html#ga26bdb7c1b6c3d4bedbcfc1774d00833c" title="Invoke Farm pattern on a data stream that can be composed in other streaming patterns. ">farm()</a></code> function may be used to create an object that may be used later in the composition. </p><hr/>
<p> <b>Example</b>: Build a farm as a composable pattern and use later as a stage of a pipeline. </p><div class="fragment"><div class="line"><span class="keyword">auto</span> print_long_words = <a class="code" href="group__farm__pattern.html#ga26bdb7c1b6c3d4bedbcfc1774d00833c">grppi::farm</a>(exec, [](<span class="keyword">auto</span> x) {</div><div class="line">  <span class="keywordflow">if</span> (x.length() &gt; 4) std::cout &lt;&lt; x &lt;&lt; std::endl;</div><div class="line">});</div><div class="line"></div><div class="line"><a class="code" href="group__pipeline__pattern__native.html#ga50396cad2879f1facd37757ea3aa564b">grppi::pipeline</a>(exec,</div><div class="line">  stageA,</div><div class="line">  stageB,</div><div class="line">  print_long_words,</div><div class="line">  stageC</div><div class="line">);</div></div><!-- fragment --><hr/>
<p> <b>Note</b>: For brevity we do not show here the details of other stages. </p>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
